package com.xhx.util;

import java.io.File;
import java.io.IOException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class OpenDirUtil {

	private static String drive;

	static {
		File currentJarFile = new File(OpenDirUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath());
		// 如果是打包成jar运行，则currentJarFile指向的是jar包；如果是直接运行class文件，则指向的是class文件。
		String jarPath = currentJarFile.getAbsolutePath(); // 获取所在盘符
		drive = jarPath.substring(0, 2); // Windows下路径形如 C:\path\to\jar，所以前2个字符就是盘符
	}

	private OpenDirUtil() {
	}

	/**
	 * 打开文件夹
	 * 
	 * @param path
	 */
	public static void openDir(String path) {
		String target;
		if (path.contains(":")) {
			target = path;
		} else {
			target = drive + path;
		}
		try {
			String osName = System.getProperty("os.name");
			if (osName != null) {
				if (osName.contains("Mac")) {
					Runtime.getRuntime().exec("open " + target);
				} else if (osName.contains("Windows")) {
					Runtime.getRuntime().exec("cmd /c start " + target);
				} else {
					log.debug("文件输出目录:" + drive + path);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
